<?php

namespace interface\v1;

/*
*OA首页接口类
*/

class oaIndexAppClass extends index
{
    private const title = 'OA首页接口类';
	/**
	 * 获取小程序端图标
	 *
	 * @param $name
	 * @return string
	 */
	public static function icon($name): string
	{
		return root . 'img/miniProgram/oa/' . $name . '.png';
	}
	
	/*列表页*/
	public function home()
	{
		$data['result']['menu'] = [
			[
				'name'  => '考勤',
				'ico'   => self::icon('clock'),
				'url'   => '/pages/clock/clock',
				'power' => ['workSign'],
			],
			[
				'name'  => '审批',
				'ico'   => self::icon('audit'),
				'url'   => '/pages/audit/audit',
				'power' => ['initiateApproval'],
			],
			[
				'name'  => '汇报',
				'ico'   => self::icon('report'),
				'url'   => '/pages/workReport/workReport',
				'power' => ['workReportDay', 'workReportWeek', 'workReportMonth'],
			],
			[
				'name'  => '考试',
				'ico'   => self::icon('examine'),
				'url'   => '/pages/exam/exam',
				'power' => ['beTested'],
			],
			[
				'name'  => '工资条',
				'ico'   => self::icon('payslip'),
				'url'   => '/pages/paySlip/paySlip',
				'power' => ['paySlip'],
			],
            [
                'name'  => '食堂',
                'ico'   => self::icon('diningHall'),
                'url'   => '/pages/canteen/canteen',
                'power' => ['all'],
            ],
		];
		
		$userPower = self::getUserPower();
		foreach ($data['result']['menu'] as $index => $array) {
			$flag = false;
			foreach ($array['power'] as $val) {
				if (array_key_exists($val, $userPower) || $val ==='all') {
					$flag = true;
					break;
				}
			}
			if (!$flag) {
				unset($data['result']['menu'][$index]);
			}
		}
		
		//待办事项
		$all = find(array('table' => 'backlog', 'where' => "stid = '" . self::$stid . "' and workFlow = '待处理' and startDay <= '" . $this->date . "' order by FIELD(urging,'是','否'),startDay desc limit 10"));
		foreach ($all['array'] as $array) {
			if (empty($array['targetCla']) && empty($array['targetFun'])) {
				$audit           = query('audit', "id = '$array[typeId]'");
				$array['typeId'] = $audit['targetId'];
			}
			$data['result']['backlog'][] = array(
				"id"     => $array['id'],//待办事项ID
				"type"   => $array['type'],//类型
				"typeId" => $array['typeId'],//对象表ID
				"text"   => $array['text'],//待办事项说明
				'urging' => $array['urging'],
				'url'    => self::navigateTo($array['type'], $array['typeId']),
				"time"   => $array['time'],//创建时间
			);
		}
		//未读消息
		$all = find(array('table' => 'message', 'where' => "stid = '" . self::$stid . "'  and see = '否' order by time desc limit 10"));
		foreach ($all['array'] as $array) {
			if (empty($array['targetCla']) && empty($array['targetFun'])) {
				$audit           = query('audit', "id = '$array[typeId]'");
				$array['typeId'] = $audit['targetId'];
			}
			$data['result']['message'][] = array(
				"id"     => $array['id'],//待办事项ID
				"type"   => $array['type'],//类型
				"typeId" => $array['typeId'],//对象表ID
				"text"   => $array['text'],//待办事项说明
				'url'    => self::navigateTo($array['type'], $array['typeId']),
				"time"   => $array['time'],//创建时间
			);
		}
		//返回
		$this->code         = 1;
		$this->resp['data'] = $data;
	}
	
	/**
	 * 待办消息跳转
	 *
	 * @param $type
	 * @param $id
	 * @return string
	 */
	public static function navigateTo($type, $id): string
	{
		switch ($type) {
            case '考试试卷':
				$url = '/pages/exam/exam';
				break;
            case '补卡申请':
				$info = query('work_sign_add', "id = '$id'");
				$url  = '/pages/workSignAddInfo/workSignAdInfo?id=' . $info['id'];
				break;
            case '请假申请':
				$info = query('work_holiday', "id = '$id'");
				
				$url = '/pages/workHolidayInfo/workHolidayInfo?id=' . $info['id'];
				break;
            case '外出申请':
            case '外出成果审核':
				$info = query('work_out', "id = '$id'");
				$url  = '/pages/workOutInfo/workOutInfo?id=' . $info['id'];
				break;
            case '加班申请':
            case '加班成果审核':
				$info = query('work_add', "id = '$id'");
				$url  = '/pages/workInfo/workInfo?id=' . $info['id'];
				break;
            case '出差申请':
            case '出差成果审核':
                $info = query('bussiness_trip', "id = '$id'");
                $url  = '/pages/bussinessTripDetail/bussinessTripDetail?id=' . $info['id'];
                break;
            case '费用申请':
				$info = query('cost', "id = '$id'");
				$url  = '/pages/feeApplyInfo/feeApplyInfo?id=' . $info['id'];
				break;
            case '费用报销':
				$info = query('cost', "id = '$id'");
				$url  = '/pages/costInfo/costInfo?id=' . $info['id'];
				break;
            case '付款申请':
				$info = query('payment', "id = '$id'");
				$url  = '/pages/paymentInfo/paymentInfo?id=' . $info['id'];
				break;
			
			default:
				$url = '';
				break;
		}
		return $url;
	}
	
	/**
	 * 功能菜单
	 */
	public function functionMenu()
	{
		$userPower              = self::getUserPower();
		$data['result']['menu'] = [
			[
				'name'  => '人力资源',
				'array' => [
					[
						'name'  => '补卡申请',
						'ico'   => self::icon('workSignAdd'),
						'power' => 'workSignAdd',
						'url'   => '/pages/workSignAdd/workSignAdd',
					],
					[
						'name'  => '请假申请',
						'ico'   => self::icon('holiday'),
						'power' => 'work',
						'url'   => '/pages/workHoliday/workHoliday',
					],
					[
						'name'  => '外出申请',
						'ico'   => self::icon('out'),
						'power' => 'workOut',
						'url'   => '/pages/workOut/workOut',
					],
					[
						'name'  => '加班申请',
						'ico'   => self::icon('workAdd'),
						'power' => 'workAdd',
						'url'   => '/pages/work/work',
					],
                    [
                        'name'  => '出差申请',
                        'ico'   => self::icon('bussinessTrip'),
                        'power' => 'bussinessTrip',
                        'url'   => '/pages/bussinessTrip/bussinessTrip',
                    ],
					[
						'name'  => '日报',
						'ico'   => self::icon('dayReport'),
						'power' => 'workReportDay',
						'url'   => '/pages/dayReportEdit/dayReportEdit',
					],
					[
						'name'  => '周报',
						'ico'   => self::icon('weekReport'),
						'power' => 'workReportWeek',
						'url'   => '/pages/weekReportEdit/weekReportEdit',
					],
					[
						'name'  => '月报',
						'ico'   => self::icon('monthReport'),
						'power' => 'workReportMonth',
						'url'   => '/pages/monthReportEdit/monthReportEdit',
					],
					[
						'name'  => '考试',
						'ico'   => self::icon('examine'),
						'power' => 'beTested',
						'url'   => '/pages/exam/exam',
					],
					[
						'name'  => '考勤',
						'ico'   => self::icon('clock'),
						'power' => 'workSign',
						'url'   => '/pages/clock/clock',
					],
					[
						'name'  => '工资条',
						'ico'   => self::icon('payslip'),
						'power' => 'paySlip',
						'url'   => '/pages/paySlip/paySlip',
					]
				],
			],
            [
                'name'  => '行政管理',
                'array' => [
                    [
                        'name'  => '食堂',
                        'ico'   => self::icon('diningHall'),
                        'url'   => '/pages/canteen/canteen',
                        'power' => '',
                    ],
                    [
                        'name'  => '宿舍',
                        'ico'   => self::icon('payment'),
                        'url'   => '/pages/dormitory/dormitory',
                        'power' => '',
                    ],
                    [
                        'name'  => '查寝',
                        'ico'   => self::icon('workSignAdd'),
                        'url'   => '/pages/checkLiveRecord/checkLiveRecord',
                        'power' => '',
                    ],
                ],
            ],
			[
				'name'  => '财务管理',
				'array' => [
					[
						'name'  => '费用申请',
						'ico'   => self::icon('feeApply'),
						'power' => 'feeApply',
						'url'   => '/pages/feeApply/feeApply',
					],
					[
						'name'  => '报销申请',
						'ico'   => self::icon('cost'),
						'power' => 'cost',
						'url'   => '/pages/cost/cost',
					],
					[
						'name'  => '付款申请',
						'ico'   => self::icon('payment'),
						'power' => 'payment',
						'url'   => '/pages/payment/payment',
					],
				],
			],
		];
		foreach ($data['result']['menu'] as $index => $array) {
			foreach ($array['array'] as $num => $val) {
				if (!array_key_exists($val['power'], $userPower) && !empty($val['power'])) {
					unset($data['result']['menu'][$index]['array'][$num]);
				}
			}
		}
		//返回
		$this->code         = 1;
		$this->resp['data'] = $data;
	}
	
	
}
